package com.ts.main.admin.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ts.main.admin.context.UserContext;
import com.ts.main.admin.dto.MiniRoleDto;
import com.ts.main.admin.dto.MiniRoleSearchPageDto;
import com.ts.main.admin.entity.RoleEntity;
import com.ts.main.admin.exception.RPage;
import com.ts.main.admin.mapper.RoleMapper;
import com.ts.main.admin.vo.AdminRolePartVo;
import com.ts.main.admin.vo.AdminRoleVo;
import com.ts.main.admin.vo.MiniRoleVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lk
 * @description 针对表【t_role(角色表)】的数据库操作Service实现
 * @createDate 2025-06-09 00:38:59
 */
@AllArgsConstructor
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> {

    public RPage<List<MiniRoleVo>> getMiniRolePage(MiniRoleSearchPageDto dto) {
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<MiniRoleVo> list = baseMapper.getAdminRolePage(dto);
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public MiniRoleVo getMiniRoleDetails(Integer roleId) {
        return baseMapper.getMiniRoleDetails(roleId);
    }

    public MiniRoleDto saveMiniRole(MiniRoleDto dto) {
        dto.setUserId(UserContext.getUser().getId());
        save(BeanUtil.copyProperties(dto, RoleEntity.class));
        return dto;
    }

    public Boolean updateMiniRole(MiniRoleDto dto) {
        return updateById(BeanUtil.copyProperties(dto, RoleEntity.class));
    }

    public Boolean deleteMiniRole(Integer roleId) {
        LambdaUpdateWrapper<RoleEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(RoleEntity::getIsDeleted, true)
                .eq(RoleEntity::getRoleId, roleId);
        return update(updateWrapper);
    }

    public List<AdminRolePartVo> getRoleList() {
        LambdaQueryWrapper<RoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleEntity::getIsDeleted, false);
        return BeanUtil.copyToList(list(queryWrapper), AdminRolePartVo.class);
    }

}




